package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.PmsShopCategoryDao;
import com.macro.mall.dto.PmsShopCategoryParam;
import com.macro.mall.mapper.PmsShopCategoryMapper;
import com.macro.mall.model.PmsShopCategory;
import com.macro.mall.model.PmsShopCategoryExample;
import com.macro.mall.service.PmsShopCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店铺商品分类表(PmsShopCategory)表服务实现类
 *
 * @author makejava
 * @since 2021-04-27 20:10:39
 */
@Service("pmsShopCategoryService")
public class PmsShopCategoryServiceImpl implements PmsShopCategoryService {

    @Autowired
    private PmsShopCategoryMapper shopCategoryMapper;

    @Override
    public List<PmsShopCategory> getList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);  // ?
        PmsShopCategoryExample example = new PmsShopCategoryExample();
        // example.setOrderByClause("sort desc");
        return shopCategoryMapper.selectByExample(example);
    }

    @Override
    public int add(PmsShopCategoryParam param) {
        // 新建一个实体类
        PmsShopCategory pmsShopCategory = new PmsShopCategory();
        // 添加其他元素(这里没有就不添加了)
        // 复制元素
        BeanUtils.copyProperties(param, pmsShopCategory);
        // 返回 count
        return shopCategoryMapper.insertSelective(pmsShopCategory);
    }

    @Override
    public int update(Long id, PmsShopCategoryParam param) {
        PmsShopCategory pmsShopCategory = new PmsShopCategory();
        BeanUtils.copyProperties(param, pmsShopCategory);
        pmsShopCategory.setId(Math.toIntExact(id));
        return shopCategoryMapper.updateByPrimaryKeySelective(pmsShopCategory);
    }

    @Override
    public int delete(Long id) {
        return shopCategoryMapper.deleteByPrimaryKey(Math.toIntExact(id));
    }
}
